package org.example.myframework.frame.demo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.myframework.frame.demo.domain.base.PageGenerator;
import org.example.myframework.frame.demo.domain.base.PageRo;
import org.example.myframework.frame.demo.domain.base.PageVo;
import org.example.myframework.frame.demo.domain.entity.TestTablePlus;
import org.example.myframework.frame.demo.domain.pojo.ro.TestTablePlusRo;
import org.example.myframework.frame.demo.enums.SystemExceptionEnum;
import org.example.myframework.frame.common.exception.SystemException;
import org.example.myframework.frame.demo.repository.TestTablePlusMapper;
import org.example.myframework.frame.demo.service.TestTablePlusService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import javax.annotation.Resource;

@Service
public class TestTablePlusServiceImpl extends ServiceImpl<TestTablePlusMapper, TestTablePlus> implements TestTablePlusService{

    @Resource
    private TestTablePlusMapper testTablePlusMapper;

    @Override
    public PageVo<TestTablePlus> getPage(PageRo<TestTablePlusRo> ro) {
        // 复制实体类
        // BeanUtils.copyProperties(testTablePlusPageRo, this);
        return PageGenerator.toPage(this.baseMapper.queryPage(PageGenerator.getPage(ro)));
    }

    @Override
    public Page<TestTablePlus> getBasePage(Page<TestTablePlus> page) {
        page.setSize(10L);
        page.setPages(1L);
        return this.baseMapper.selectPage(page, Wrappers.lambdaQuery(TestTablePlus.class));
    }

    @Override
    @Cacheable(cacheNames = "testTablePlus" , key = "'getByTableId'+#id")
    public TestTablePlus getByTableId(Long id) {
        return this.getById(id);
    }

    @Override
    public void throwExceptionTest() {
        throw new SystemException(SystemExceptionEnum.NOTFOUND);
    }

    @Override
    public Boolean saveEntity(TestTablePlusRo ro) {
        TestTablePlus testTablePlus = new TestTablePlus();
        BeanUtils.copyProperties(ro, testTablePlus);
        return this.save(testTablePlus);
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        return this.removeById(id);
    }
}
